package com.xxm.thoughtworksdemo

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.xxm.thoughtworksdemo.databinding.ItemTweeBinding
import com.xxm.thoughtworksdemo.databinding.ViewLoadBinding


class TweeAdapter constructor(var context: Context) : RecyclerView.Adapter<TweeAdapter.VH>() {

    private var list:ArrayList<TweeBean>? = null

    private val typeData = 0
    private val typeLoad = 1

    var mLoadMoreEnable = true
    set(value) {
        field=value
        notifyDataSetChanged()
    }

    var isLoad=false

    var mOnLoadMoreListener: OnLoadMoreListener? = null

    class VH : RecyclerView.ViewHolder{
        var binding: ItemTweeBinding?=null
        constructor(binding: ItemTweeBinding):super(binding.root){
            this.binding=binding
        }
        constructor(view: View):super(view)
    }

    interface OnLoadMoreListener {
        fun onLoadMore()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VH {
        return if(viewType==typeData){
            VH(ItemTweeBinding.inflate(LayoutInflater.from(context), parent, false))
        }else{
            VH(ViewLoadBinding.inflate(LayoutInflater.from(context), parent, false).root)
        }
    }

    override fun onBindViewHolder(holder: VH, position: Int) {
        if(getItemViewType(position)==typeLoad){
            if(mOnLoadMoreListener != null && mLoadMoreEnable && !isLoad) {
                isLoad=true
                mOnLoadMoreListener?.onLoadMore()
                isLoad=false
            }
        }else{
            val tweeBean = list?.get(position)
            if(tweeBean?.sender?.username.isNullOrEmpty()){
                holder.binding!!.userText.visibility=View.GONE
            }else{
                holder.binding!!.userText.visibility=View.VISIBLE
                holder.binding!!.userText.text= tweeBean?.sender?.username
            }
            Glide.with(context)
                .load(tweeBean?.sender?.avatar)
                .apply(RequestOptions().centerCrop())
                .transition(DrawableTransitionOptions.withCrossFade())
                .into(holder.binding!!.userImg)

            if(tweeBean?.content.isNullOrEmpty()){
                holder.binding!!.contentText.visibility=View.GONE
            }else{
                holder.binding!!.contentText.visibility=View.VISIBLE
                holder.binding!!.contentText.text = tweeBean?.content
            }

            if(tweeBean?.images!=null && tweeBean.images.isNotEmpty()){
                if(tweeBean.images.size>1){
                    holder.binding!!.gridView.visibility=View.VISIBLE
                    holder.binding!!.imageImg.visibility=View.GONE
                    holder.binding!!.gridView.adapter=ImagesAdapter(context, tweeBean.images)
                }else{
                    holder.binding!!.imageImg.visibility=View.VISIBLE
                    holder.binding!!.gridView.visibility=View.GONE
                    Glide.with(context)
                        .load(tweeBean.images[0].url)
                        .into(holder.binding!!.imageImg)
                }
            }

            if(tweeBean?.comments!=null && tweeBean.comments.isNotEmpty()){
                holder.binding!!.commentView.visibility=View.VISIBLE
                holder.binding!!.commentView.addComments(tweeBean.comments)
            }else{
                holder.binding!!.commentView.visibility=View.GONE
            }
        }
    }

    override fun getItemCount(): Int {
        return if (list == null) {
            0
        }else{
            list?.size?.plus(if (mLoadMoreEnable) 1 else 0)!!
        }
    }

    override fun getItemViewType(position: Int): Int {
        return if (!mLoadMoreEnable || position < list?.size!!){
            typeData
        }else{
            typeLoad
        }
    }

    fun setList(twees: List<TweeBean>){
        list= arrayListOf()
        list?.addAll(twees)
        mLoadMoreEnable=true
    }

    fun addList(twees: List<TweeBean>){
        if(list==null){
            list= arrayListOf()
        }
        list?.addAll(twees)
        notifyDataSetChanged()
    }
}